package com.fbs.wowo.manage.web;

import com.fbs.wowo.base.BaseController;
import com.fbs.wowo.base.Response;
import com.fbs.wowo.manage.entity.UserLogin;
import com.fbs.wowo.manage.service.LoginService;
import com.fbs.wowo.manage.service.VerifyCodeService;
import com.fbs.wowo.manage.vo.UserLoginVoPC;
import com.fbs.wowo.util.netutil.NetUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理登录， 注册，登出， 注销
 *
 */
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {

    /**
     * 登录过程
     */
    /**
     * 发送验证码
     * @return
     */
    @PostMapping("/tel")
    public Response generateVerifyCode(@RequestParam String tel) {
        return success(loginService.sendVerifyCodeLogin(tel));
    }

    /**
     * 手机 老用户登录
     * @param userLogin
     * @param request
     * @return
     */
    @PostMapping("/signin")
    public Response login(@ModelAttribute UserLogin userLogin, HttpServletRequest request) {
        return success(loginService.loginMob(userLogin, NetUtils.getIpAddress(request)));
    }



    /**
     * 手机 注册
     * @return
     */
    @PostMapping("/signup")
    public Response register(@ModelAttribute UserLogin userLogin, HttpServletRequest request) {
        return success(loginService.regMob(userLogin, NetUtils.getIpAddress(request)));
    }

    /**
     * 电脑端登录
     */
    @PostMapping("/signinPC")
    public Response login(@ModelAttribute UserLoginVoPC userLoginVoPC, HttpServletRequest request) {
        // 跳转成功页面
        return success(loginService.loginPC(userLoginVoPC, NetUtils.getIpAddress(request)));
    }

    /**
     * 电脑端注册
     */
    @PostMapping("/signupPC")
    public Response signup(@ModelAttribute UserLoginVoPC userLoginVoPc, HttpServletRequest request) {
        return success(loginService.regPC(userLoginVoPc, NetUtils.getIpAddress(request)));
    }

    /**
     * 电脑端登出
     */
    @PostMapping("/logoutPC")
    public Response logoutPC() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null)
            subject.logout();
        return success("退出成功");
    }

    /**
     * 发送验证码
     */
    @PostMapping("/forgetVerifyCode")
    public Response forgetVerifyCode(String tel) {
        return success(verifyCodeService.sendVerifyCode(tel));
    }

    /**
     * 电脑端找回密码步骤：
     * 发送验证码
     * 修改密码成功
     * 重新登录
     */
    @PostMapping("/forget")
    public Response forget(@RequestParam String tel, @RequestParam String verifyCode, @RequestParam String newPassword) {
        loginService.forget(tel, verifyCode, newPassword);
        return success();
    }

    @Autowired
    private LoginService loginService;
    @Autowired
    private VerifyCodeService verifyCodeService;
}
